package xyz.xpman.prisonerfitness.entity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;

import androidx.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import java.time.LocalDate;
import java.util.Date;
import java.util.Map;

import xyz.xpman.prisonerfitness.kit.DateKit;


@SuppressWarnings("unused")
public class TrainingThoughts extends BaseModel<TrainingThoughts> {
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    private static final String DATE = "date";
    private static final String THOUGHTS = "thoughts";

    public static final Map<String, Class<?>> attrs = Map.ofEntries(
            Map.entry(ID, Integer.class),
            Map.entry(DATE, String.class),
            Map.entry(THOUGHTS, String.class));


    private static boolean init = false;

    public static final String TABLE = "training_thoughts";



    public static void init(@Nullable Context context) {
        if (!init) {
            init = true;
            bind(new TableInfo(TABLE, attrs, TrainingThoughts.class, ID));
        }
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public TrainingThoughts findByDate(@NotNull LocalDate date) {
        return findByDate(DateKit.format(date, DATE_FORMAT));
    }

    private TrainingThoughts findByDate(@NotNull String date) {
        final String sqlFmt = "select * from %s where date = ?";
        final String sql = String.format(sqlFmt, table.name);

        @SuppressLint("Recycle")
        Cursor cursor = db.rawQuery(sql, new String[]{ date });
        cursor.moveToFirst();

        return processCursor(cursor, this::byCursor);
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public static TrainingThoughts byThoughts(String thoughts) {
        TrainingThoughts model = new TrainingThoughts();

        model.setDate(new Date());
        model.setThoughts(thoughts);

        return model;
    }

    public static TrainingThoughts by(String thoughts, LocalDate date) {
        TrainingThoughts model = new TrainingThoughts();

        model.setDate(date);
        model.setThoughts(thoughts);

        return model;
    }


    // getter and setter
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public Integer getId() { return getInteger(ID); }

    public void setId(Integer id) { set(ID, id); }



    public String getDate() {
        return getString(DATE);
    }

    @SuppressLint("NewApi")
    public void setDate(Date date) {
        set(DATE, DateKit.format(date, DATE_FORMAT));
    }

    @SuppressLint("NewApi")
    public void setDate(LocalDate date) {
        set(DATE, DateKit.format(date, DATE_FORMAT));
    }



    public String getThoughts() {
        return getString(THOUGHTS);
    }

    public void setThoughts(String thoughts) {
        set(THOUGHTS, thoughts);
    }

}
